package com.rinvay.asdownloader.ui;

import java.util.ArrayList;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.rinvay.asdownloader.R;
import com.rinvay.network.filedownload.FileDownloadManager;
import com.rinvay.network.filedownload.model.DownloadTask;
import com.rinvaylabs.utils.FileUtil;
import com.rinvaylabs.utils.MIMEUtil;

public class DownloadAdapter extends BaseAdapter
{
    private Context context;
    private ArrayList<DownloadTask> displayed;
    private FileDownloadManager downloadMgr;

    public DownloadAdapter(Context context, ArrayList<DownloadTask> list, FileDownloadManager mgr) {
        this.context = context;
        displayed = list;
        downloadMgr = mgr;
    }

    public int getCount() {
        if (displayed != null) {
            return displayed.size();
        }
        return 0;
    }

    public Object getItem(int position) {
        if (displayed != null && position < getCount()) {
            return displayed.get(position);
        }
        return null;
    }

    public long getItemId(int position) {

        return 0;
    }

    public View getView(int position, View v, ViewGroup parent) {
        if (v == null) {
            ViewHolder holder = new ViewHolder();
            v = View.inflate(context.getApplicationContext(), R.layout.list_item,
                    null);

            holder.type = (ImageView) v.findViewById(R.id.res_type);
            holder.name = (TextView) v.findViewById(R.id.res_name);
            holder.progress = (ProgressBar) v
                    .findViewById(R.id.res_progress);
            holder.info = (TextView) v.findViewById(R.id.task_info);
//            holder.operate = (ImageView) v.findViewById(R.id.task_control);

            v.setTag(holder);

        }

        ViewHolder holder = (ViewHolder) v.getTag();
        final DownloadTask dt = (DownloadTask) getItem(position);
        if (dt == null) {
            return v;
        }
        holder.type.setImageResource(getResourceType(dt.getFileName()));
        holder.type.setOnClickListener(new OnClickListener() {
            
            public void onClick(View v) {
                System.out.println("click");
                if(dt.isTaskFinished()) {
                    return;
                }
                if(dt.isDownloading()) {
                    System.out.println("pause");
                    downloadMgr.pauseResouseDownload(dt);
                } else {
                    System.out.println("resume");
                    downloadMgr.resumeResourceDownload(dt);
                }
            }
        });
        holder.name.setText(dt.getFileName());

        if (dt.isTaskFinished()) {
            holder.progress.setVisibility(View.GONE);
        } else {
            holder.progress.setVisibility(View.VISIBLE);
        }
        if (dt.getSize() <= 0) {
            holder.progress.setProgress(0);
        } else {

            int progress = dt.getCurLength() * 100 / dt.getSize();
            holder.progress.setProgress(progress);
        }

        holder.info.setText(getRTInfo(dt));
//        holder.operate.setOnClickListener(new OnClickListener() {
//            public void onClick(View v) {
//                downloadMgr.startResourceDownload(dt);
//            }
//        });

        return v;
    }

    /**
     * 实时任务信息
     * @return
     */
    private String getRTInfo(DownloadTask task) {
        if(DownloadTask.STATE_WAITING == task.getState()) {
            return "排队";
        }
        StringBuffer buffer = new StringBuffer();
        if (task.getSize() > 0) {
            buffer.append(FileUtil.FormetFileSize(task.getSize()));
        } else {
            buffer.append("未知大小");
        }
        if (task.getState() != DownloadTask.STATE_DOWNLOADING) {
            return buffer.toString();
        }
        buffer.append(" ").append(task.getSpeed()).append("k/s");

        return buffer.toString();
    }

    private int getResourceType(String name) {
        name = name.toLowerCase();
        int resId;
        String mime = MIMEUtil.getMimeType(MIMEUtil
                .getFileExtension(name));
        if (MIMEUtil.isAudioType(mime)) {
            resId = R.drawable.music;
        } else if (MIMEUtil.isImageType(mime)) {
            resId = R.drawable.pictrue;
        } else if (MIMEUtil.isVideoType(mime)) {
            resId = R.drawable.video;
        } else {
            resId = R.drawable.other;
        }

        return resId;
    }

    class ViewHolder
    {
        /**
         * 资源类型，图片，音频，视频。。。
         */
        public ImageView type;
        /**
         * 资源名称
         */
        public TextView name;
        /**
         * 下载进度
         */
        public ProgressBar progress;
        /**
         * 下载信息
         */
        public TextView info;
        /**
         * 下载控制
         */
//        public ImageView operate;
    }

}
